<?php 
/**********************************INCLUDE*********************************** *
* **************************************************************************** */
include_once '../app/App.php';
include_once '../account/Account.php';
include_once '../db/DatabaseManager.php';

/**
 * @author nicholas malacarne <nicholas.malacarne@gmail.com>, kayvan sarikhani <ksarikhani@gmail.com>
 * 
 * This script is used to log an account into the app.
 */

// required post data fields -nm
$required = array( 'username', 'password' );

try {
    
    // check if we have required parameters -nm
    if ( ! App::paramOK( $required, $_GET ) ){
        
        throw new ErrorException( "Error Processing Parameters !" );
    }
    
    // get instance of App -nm
    $app = App::init();
    
    // create temporary 'db' array for testing -nm
    $db = new DatabaseManager( 'db.refinedguitar.com', 'troop410', 'GTCtr00p410!', 'troop410' );
    
    // strip post data -nm
    $username  = filter_input( INPUT_GET, 'username' );
    $password  = filter_input( INPUT_GET, 'password' );
    
    // double check to make sure username/password values meet min length -nm
    if ( strlen( $username ) >= 5 && strlen( $password ) >= 5 ){
        
        // extract account data -nm
        $accountData = $db->getRowByValue( "accounts", "email", $username );
        
        // if account data is found -nm
        if ( isset( $accountData ) ){
            
            // TODO: check password -nm
            // TODO: log account into app -nm
            
        }else{
            
            // no account data found -nm
            echo json_encode( array( "invalidData" => "Invalid Username/Password" ) );
        }
        
    }else{
        
        echo json_encode( array( "invalidData" => "Invalid Username/Password" ) );
    }
    
}catch( Exception $e ){
    
    $report[ 'exception' ] = array();
    
    $report[ 'exception' ][ 'file' ] = $e->getFile();
    $report[ 'exception' ][ 'line' ] = $e->getLine();
    $report[ 'exception' ][ 'msg' ] = $e->getMessage();
    
    echo json_encode( $report );
    
} // TODO: handle specific exception -nm

exit();
